package toDo.app.controllers;

import interfaces.IRegisterActivity;
import toDo.app.R;
import toDo.app.models.User;
import database.DatabaseFacade;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class RegisterActivityClickController implements OnClickListener {
	private IRegisterActivity activity;

	public RegisterActivityClickController(IRegisterActivity activity) {
		this.activity = activity;
	}

	@Override
	public void onClick(View v) {

		switch(v.getId()) {
		// determine what has been clicked, both are have the onClick=...
			case R.id.registerRegisterButton:
				processRegisterEvent();
				break;
			case R.id.registerBackButton:
				processBackEvent();
				break;
		}
	}

	public void processRegisterEvent() {
		User user = DatabaseFacade.addUser(activity, new User(activity.getUsername(), activity.getPassword()));
		// pass in a user
		if (user.isValid()) {
			// user is valid means was added add user parcelable to send
			activity.setResult(IRegisterActivity.RESULT_OK, User.putUserIntoIntent(new Intent(), user));
			// set result
			activity.finish();// return the data to central manager
		} else {// else this, currently it doesn't actually check taken
				// properly, but that's not part of M6
			activity.setStatusLabelText(activity.getString(R.string.userTaken));
			new AlertDialog.Builder(activity).setTitle(R.string.error).setMessage(R.string.userTaken)
					.setNeutralButton(R.string.ok, null).show();

		}
	}

	public void processBackEvent() {
		activity.setResult(IRegisterActivity.RESULT_CANCELED);
		activity.finish();
	}
}
